package hapi.geom;
import hapi.math.Vector2D;

class AABB {

	public var l:Float;
	public var b:Float;
	public var r:Float;
	public var t:Float;

	public var width(getWidth,null):Float;
	public var height(getHeight,null):Float;
	
	public function new(left,top,right,bottom) {
		this.l = left;
		this.t = top;
		this.r = right;
		this.b = bottom;
	}

	public function intersects( aabb : AABB ) {
		return !(aabb.l > r || aabb.r < l || aabb.t > b || aabb.b < t);
	}

	public inline function contains( v:Vector2D ) {
		return !(v.y < t || v.y > b || v.x < l || v.x > r);
	}
	
	public inline function project( p:hapi.math.Vector2D ){
		if( p.x < l ) p.x = l;
		else if( p.x > r ) p.x = r;
		if( p.y > t ) p.y = t;
		else if( p.y < b ) p.y = b;
	}
	
	function getWidth(){
		return Math.abs( r - l );
	}
	
	function getHeight(){
		return Math.abs( b - t );
	}

	public function toString() {
		return "[l=" + l + " b=" + b + " r=" + r + " t=" + t + "]";
	}

}